package com.ruoyi.web.controller.finance;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.finance.domain.vo.PaymentRequestVo;
import com.ruoyi.finance.service.IPaymentRequestService;
import com.ruoyi.finance.service.IPaymentVoucherService;
import com.ruoyi.procured.domain.vo.PaymentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 申请款Controller
 * 
 * @author liuguangfan
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/finance/paymentRequest")
public class PaymentRequestController extends BaseController
{
    @Autowired
    private IPaymentRequestService paymentRequestService;

    @Autowired
    private IPaymentVoucherService paymentVoucherService;

    /**
     * 查询申请款列表
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentRequest:list')")
    @GetMapping("/getPaymentRequestList")
    public TableDataInfo list(PaymentRequestVo paymentRequestVO)
    {
        startPage();
        List<com.ruoyi.finance.domain.vo.PaymentRequestVo> list = paymentRequestService.selectPaymentRequestList(paymentRequestVO);
        return getDataTable(list);
    }
    /**
     * 查询申请款列表
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentRequest:list')")
    @GetMapping("/getPaymentVoList")
    public TableDataInfo list(PaymentVo paymentVo)
    {
        startPage();
        List<PaymentVo> list = paymentRequestService.selectPaymentVotList(paymentVo);
        return getDataTable(list);
    }
    /**
     * 驳回申请款
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentRequest:reject')")
    @PostMapping("/rejectPaymentRequest")
    public ResponseEntity<String> updatePaymentReviewStatus(@RequestParam List<String> paymentRequestIds, @RequestParam String processRemark) {

        if(paymentRequestIds.isEmpty()){
            throw new ServiceException("获取申请款ID失败。");
        }
        if(StringUtils.isNull(processRemark)){
            throw new ServiceException("驳回原因不能为空。");
        }

        paymentRequestService.rejectPaymentRequest(paymentRequestIds,processRemark,getUserId());
        // 返回响应
        return ResponseEntity.ok("状态更新成功");
    }
    /**
     * 核准申请款
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentRequest:approved')")
    @PostMapping("/approvedPaymentRequest/{paymentRequestIds}")
    public ResponseEntity<String> approvedPaymentRequest(@PathVariable List<String> paymentRequestIds) {

        if(paymentRequestIds.isEmpty()){
            throw new ServiceException("获取申请款ID失败。");
        }

        paymentRequestService.approvedPaymentRequest(paymentRequestIds,getUserId(),getNickname());
        // 返回响应
        return ResponseEntity.ok("状态更新成功");
    }

    /**
     * 生成付款单
     */
    @PreAuthorize("@ss.hasPermi('finance:paymentRequest:voucher')")
    @PostMapping("/voucherPaymentRequest/{paymentRequestIds}")
    public ResponseEntity<String> voucherPaymentRequest(@PathVariable List<String> paymentRequestIds) {

        if(paymentRequestIds.isEmpty()){
            throw new ServiceException("获取申请款ID失败。");
        }

        paymentVoucherService.voucherPaymentRequest(paymentRequestIds,getNickname());
        // 返回响应
        return ResponseEntity.ok("状态更新成功");
    }
}
